﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;
using System.Linq;
using System.Text;
using DotNetNukeMVPWizard.ProjectConfiguration;

namespace DotNetNukeMVPWizard
{
    public class IWizardImplementation : IWizard
    {
        #region Constructors
        public IWizardImplementation()
            : this(new ProjectConfigurationForm(), CustomPlaceholders)
        {
        }

        internal IWizardImplementation(IConfigurationForm inputForm, IMvpMultiProjectConfiguration customPlaceholders)
        {
            if (customPlaceholders == null)
                CustomPlaceholders = new MvpMultiProjectConfiguration();
            else
                CustomPlaceholders = customPlaceholders;
            InputForm = inputForm;
        }
        #endregion

        #region Properties
        internal IConfigurationForm InputForm { get; set; }
        internal static IMvpMultiProjectConfiguration CustomPlaceholders { get; set; }
        #endregion

        #region IWizard Interface Implementations
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            if (runKind == WizardRunKind.AsMultiProject && runKind != WizardRunKind.AsNewProject)
            {
                // Set some initial defaults
                CustomPlaceholders.SetParentProjectName(replacementsDictionary["$projectname$"]);
                CustomPlaceholders.SetIisAppRootUrl(@"http://localhost/DotNetNuke_Community");
                CustomPlaceholders.SetPathIisAppRootUrl(@"C:\DotNetNuke\DotNetNuke_5_Maintenance\Website");
                CustomPlaceholders.SetYourCompany("Your Company");
                CustomPlaceholders.SetProjectNamespaceName(CustomPlaceholders.SafeYourCompany + ".Modules");

                InputForm.Configuration = CustomPlaceholders;

                DialogResult inputFormShowDialog = InputForm.ShowDialog();
                if (inputFormShowDialog == DialogResult.OK)
                {
                    string wizardCancelledReason = "The dialog form did not return OK, but the custom placeholders for the template were not all valid";
                    FillDictionaryWithConfiguration(replacementsDictionary, wizardCancelledReason);
                }
                else
                    throw new WizardCancelledException("Multi-Project Template creation cancelled by the user");
            }
            else if (runKind == WizardRunKind.AsNewProject)
            {
                FillDictionaryWithConfiguration(replacementsDictionary, "The custom placeholders for the template are not all valid");
            }
        }

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        // This method is only called for item templates, not for project templates
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
            //throw new NotImplementedException();
        }
        #endregion

        #region Private Helper Methods
        private static void FillDictionaryWithConfiguration(Dictionary<string, string> replacementsDictionary, string wizardCancelledReason)
        {
            if (CustomPlaceholders.IsValid)
                CustomPlaceholders.FillDictionary(replacementsDictionary);
            else
            {
                throw new WizardCancelledException(wizardCancelledReason);
            }
        }
        #endregion
    }
}
